/*
 * Copyright (c) 2017-present, CV4J Contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.image.util;

import java.util.List;
import java.util.Map;

/**
 * 先决条件
 *
 * @author dev
 * @date 2021/07/22
 */
public class Preconditions {
    /**
     * 是空白
     * 可以判断任何一个对象是否为空,包括List Map String 复杂对象等等,
     * 只能判断对象,而不能判断基本数据类型
     * Preconditions.isBlank("")  true
     * Preconditions.isBlank(" ")  true
     * Preconditions.isBlank(null)  true
     * Preconditions.isBlank("null")  false
     *
     * @param t1 t1
     * @return boolean
     */
    public static <T> boolean isBlank(T t1) {
        if (t1 == null) {
            return true;
        }
        if (t1 instanceof List) {
            if (((List) t1).size() == 0) {
                return true;
            }
        } else if (t1 instanceof Map) {
            if (((Map) t1).size() == 0) {
                return true;
            }
        } else if (t1 instanceof Object[]) {
            if (((Object[]) t1).length == 0) {
                return true;
            }
        } else if (t1 instanceof String) {
            String str = (String) t1;
            if (str.length() == 0) {
                return true;
            }
            str = str.trim();
            if (str.length() == 0) {
                return true;
            }
        }else {
            return false;
        }
        return false;
    }

    /**
     * 不是空白
     *
     * @param t1 t
     * @return boolean
     */
    public static <T> boolean isNotBlank(T t1) {
        return !isBlank(t1);
    }

    /**
     * 不是空白
     *
     * @param objects 对象
     * @return boolean
     */
    public static <T> boolean isNotBlanks(T... objects) {
        if (objects == null) {
            return false;
        }
        for (T obj : objects) {
            if (isBlank(obj)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 检查非空
     *
     * @param arg 参数
     * @return {@link T}
     */
    public static <T> T checkNotNull(T arg) {
        return checkNotNull(arg, "Argument must not be null");
    }

    /**
     * 检查非空
     *
     * @param arg 参数
     * @param message 消息
     * @return {@link T}
     */
    public static <T> T checkNotNull(T arg, String message) {
        if (arg == null) {
            throw new NullPointerException(message);
        }
        return arg;
    }
}
